package com.xyazm.tms.api;

import com.xyazm.common.core.JsonResult;
import com.xyazm.common.utils.ParamCheckUtil;
import com.xyazm.tms.domain.dto.SendOutDTO;
import com.xyazm.tms.domain.param.SendOutParam;
import com.xyazm.tms.exception.TmsBizException;
import com.xyazm.tms.service.ITmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 物流服务对外暴露的接口实现类
 * @author xuyou
 * 2022/7/31 0031 21:53
*/
@Slf4j
@DubboService(version = "1.0.0",interfaceClass = ITmsApi.class,retries = 0)
public class TmsApi implements ITmsApi {

    @Autowired
    private ITmsService tmsService;

    @Override
    public JsonResult<SendOutDTO> sendOut(SendOutParam param) {
        try {
            ParamCheckUtil.checkObjectNonNull(param);
            tmsService.sendOut(param);
            return JsonResult.buildSuccess();
        } catch (TmsBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<Void> cancelSendOut(String orderId) {
        try {
            ParamCheckUtil.checkObjectNonNull(orderId);
            tmsService.cancelSendOut(orderId);
            return JsonResult.buildSuccess();
        } catch (TmsBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
